using B.S.NewMedical.Api.Read.Application.Command.MobileService.Consult;
using B.S.NewMedical.Api.Read.Application.Command.Order;
using B.S.NewMedical.Api.Read.DTOS;
using B.S.NewMedical.Api.Read.DTOS.Consult;
using B.S.NewMedical.Domain.Order;
using B.S.NewMedical.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace B.S.NewMedical.Api.Read.Controllers
{
    /// <summary>
    /// 健康咨询控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class ConsultController : ControllerBase
    {
        private readonly IMediator _mediator;

        public ConsultController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 获取健康咨询分类列表
        /// </summary>
        /// <returns>分类列表</returns>
        [HttpGet("categories")]
        public async Task<ApiResult<List<ConsultCategoryDto>>> GetCategories()
        {
            return await _mediator.Send(new GetConsultCategoriesCommand());
        }

        /// <summary>
        /// 获取健康咨询问题列表（分页）
        /// </summary>
        /// <param name="catId">分类ID，可选</param>
        /// <param name="keyword">关键词，可选</param>
        /// <param name="pageIndex">页码，默认1</param>
        /// <param name="pageSize">每页条数，默认10</param>
        /// <returns>问题列表分页数据</returns>
        [HttpGet("questions")]
        public async Task<ApiResult<PagedList<ConsultQuestionDto>>> GetQuestions(
            [FromQuery] int? catId = null,
            [FromQuery] string keyword = null,
            [FromQuery] int pageIndex = 1,
            [FromQuery] int pageSize = 10)
        {
            return await _mediator.Send(new GetConsultQuestionsCommand(catId, keyword, pageIndex, pageSize));
        }

        /// <summary>
        /// 获取健康咨询详情
        /// </summary>
        /// <param name="questionId">问题ID</param>
        /// <returns>咨询详情</returns>
        [HttpGet("detail/{questionId}")]
        public async Task<ApiResult<ConsultDetailDto>> GetDetail(int questionId)
        {
            return await _mediator.Send(new GetConsultDetailCommand(questionId));
        }

        /// <summary>
        /// 获取问诊单分页数据
        /// </summary>
        /// <param name="index">页码，默认1</param>
        /// <param name="size">页大小，默认10</param>
        /// <param name="patient">患者姓名，可选</param>
        /// <param name="method">问诊方式，可选</param>
        /// <param name="source">来源，可选</param>
        /// <param name="status">问诊状态，可选</param>
        /// <param name="orderStatus">订单状态，可选</param>
        /// <returns>问诊单分页数据</returns>
        [HttpGet("consultations")]
        public async Task<ApiPageing<ConsultationModel>> GetConsultations(
            [FromQuery] int index = 1,
            [FromQuery] int size = 10,
            [FromQuery] string patient = null,
            [FromQuery] string method = null,
            [FromQuery] string source = null,
            [FromQuery] string status = null,
            [FromQuery] string orderStatus = null)
        {
            return await _mediator.Send(new ConsultationPageCommand
            {
                Index = index,
                Size = size,
                Patient = patient,
                Method = method,
                Source = source,
                Status = status,
                Orderstatus = orderStatus
            });
        }

        /// <summary>
        /// 获取用户交互状态（点赞/收藏/追问）
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="objType">对象类型(question/answer)</param>
        /// <param name="objId">对象ID</param>
        /// <returns>用户交互状态</returns>
        [HttpGet("GetUserInteractStatus")]
        public async Task<ApiResult<UserInteractStatusDto>> GetUserInteractStatus(
            [FromQuery] int userId,
            [FromQuery] string objType,
            [FromQuery] int objId)
        {
            return await _mediator.Send(new GetUserInteractStatusCommand(userId, objType, objId));
        }
    }
} 